﻿using System;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using Jungle.Code.Core;
using System.Collections.Generic;
using Jungle.Code.DAL;

using Jungle.Rent;
using Jungle.Users;


    public partial class OffersAdmin : BaseAdminModule
    {
        public override void FillBaseInfo()
        {
            sysName = "Offers";
        }
        public override void Main()
        {
        }
        // ------------------------------------------------------------------------------------------
        protected override void OnInit(EventArgs e)
        {
            SessionKeeper sk = new SessionKeeper();
            sk.SessionId = Session.SessionID;
            sk.GetData();

            switch (Request["cmd"])
            { 
                case "view":
                    OfferMenu.ActiveViewIndex = 1;
                    LoadData();
                    break;
                case "edit":
                    OfferMenu.ActiveViewIndex = 3;
                    LoadForEdit();
                    break;
                default:
                    OfferMenu.ActiveViewIndex = 0;
                    break;
            }
            base.OnInit(e);
        }
        // ------------------------------------------------------------------------------------------
        protected void LoadData()
        {
            if (Request["id"] != null)
            {
                Offer off = new Offer();
                off.OfferId = Convert.ToInt32(Request["id"]);
                off.GetById();

                if (off.Photo.Length > 10) LoadImg(off.Photo);

                lb_OfferInfo.Text += "<br/><strong>Код объекта:</strong> " + Offer.GetObjectNomber(off.OfferId);
                lb_OfferInfo.Text += "<br/><strong>Количество комнат:</strong> " + off.Rooms.ToString();
                lb_OfferInfo.Text += "<br/><strong>Тип объекта:</strong> " + Offer.ddl_ObjectType().Items.FindByValue(off.ObjectType.ToString()).Text;
                try { lb_OfferInfo.Text += "<br/><strong>Подтип: </strong>" + Offer.ddl_UnderType(off.ObjectType).Items.FindByValue(off.UnderTypeId.ToString()).Text; }
                catch { }
                lb_OfferInfo.Text += "<br/><strong>Заявка на период:</strong> " + off.Period.ToString() + " дней";
                try { lb_OfferInfo.Text += "<br/><strong>Адрес: </strong>" + Offer.ddl_Region().Items.FindByValue(off.RegionId.ToString()).Text + " " + off.City + " " + off.Street + " " + off.HomeNumber; }
                catch { }
                lb_OfferInfo.Text += "<br/><strong>Цена:</strong> " + off.Price.ToString();
                lb_OfferInfo.Text += "<br/><strong>Этаж/Этажность</strong>" + off.Floor + "/" + off.Floors;
                lb_OfferInfo.Text += "<br/><strong>Материал стен: </strong>" + off.WallMaterials;
                lb_OfferInfo.Text += "<br/><strong>Материал перекрытий: </strong>" + off.PerMaterials;
                lb_OfferInfo.Text += "<br/><strong>Тип аренды: </strong>" + off.ArendType;
                lb_OfferInfo.Text += "<br/><strong>Возможное использование: </strong>" + off.AnotherUsing;
                lb_OfferInfo.Text += "<br/><strong>Нынешнее использование: </strong>" + off.NowUsing;
                lb_OfferInfo.Text += "<br/><strong>Оценка: </strong>" + off.QualityLevel.ToString();
                lb_OfferInfo.Text += "<br/><strong>Пложать: </strong>" + off.AreaTotal + "/" + off.AreaLive + "/" + off.AreaKitchen;
                lb_OfferInfo.Text += "<br/><strong>Площадь участка: </strong>" + off.FieldArea;
                lb_OfferInfo.Text += "<br/><strong>Балконы: </strong>" + off.Balcon.ToString();
                lb_OfferInfo.Text += "<br/><strong>Описание: </strong>" + off.Description;
                lb_OfferInfo.Text += "<br/><strong>Год постройки: </strong>" + off.YearBuild;
                lb_OfferInfo.Text += "<br/><strong>Статус: </strong>" + off.Status;
                lb_OfferInfo.Text += "<br/><strong>Расположение: </strong>" + off.Place;
                if(off.OfferType)lb_OfferInfo.Text += "<br/><strong>Тип заявки: </strong> Продажа"; 
                else lb_OfferInfo.Text += "<br/><strong>Тип заявки: </strong> Аренда";
                lb_OfferInfo.Text += "<br/><br/>";
                lb_OfferInfo.Text += "<br/><strong>Данные автора заявки: </strong>";
                lb_OfferInfo.Text += "<br/><strong>Ф.И.О: </strong>" + off.Name + " " + off.HPhone + " " + off.WPhone;
                lb_OfferInfo.Text += "<br/><strong>Телефоны: </strong>" + off.MPhone + ", " + off.Fax;
                lb_OfferInfo.Text += "<br/><strong>Email: </strong>" + off.Email + "<br/><br/>";
            }
        }
        // ------------------------------------------------------------------------------------------
        protected void LoadForEdit()
        {
            try
            {
                if (Request["id"] != null)
                {
                    Offer off = new Offer();
                    off.OfferId = Convert.ToInt32(Request["id"]);
                    off.GetById();

                    switch (off.ObjectType)
                    {
                        case 1: FlatForm1.Visible = true; break;
                        case 2: CommForm1.Visible = true; break;
                        case 3: HouseForm1.Visible = true; break;
                        case 4: GarageForm1.Visible = true; break;
                        case 5: GroundForm1.Visible = true; break;
                    }
                }
            }
            catch { }
        }
        // ------------------------------------------------------------------------------------------
        public void ConfirmOffer(object sender, EventArgs e)
        {
            if (Request["id"] != null)
            {
                Offer off = new Offer();
                off.OfferId = Convert.ToInt32(Request["id"]);
                off.GetById();

                off.Activation = true;
                off.PubDate = DateTime.Now;
                off.Update();

                Response.Redirect(UrlHelper.GetSiteUrl() + "/admin/Modules.aspx?mod=Offers&cmd=default");
            }
        }
        // ---------------------------------------------------------------------
        public void LoadImg(string files)
        {
            try
            {
                string[] ff = files.Split(',');
                if (ff.Length > 1)
                {
                    if (ff[1] != null)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[1]));
                        if (img.Width > 70) { img_1.Width = 70; img_1.Height = img.Height / (img.Width / 70); }
                        img_1.ImageUrl = UrlHelper.GetSiteUrl() + ff[1];
                        img_1.Visible = true;
                    }

                    if (ff[2] != null)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[2]));
                        if (img.Width > 70) { img_2.Width = 70; img_2.Height = img.Height / (img.Width / 70); }
                        img_2.ImageUrl = UrlHelper.GetSiteUrl() + ff[2];
                        img_2.Visible = true;
                    }
                    if (ff[3] != null)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[3]));
                        if (img.Width > 70) { img_3.Width = 70; img_3.Height = img.Height / (img.Width / 70); }
                        img_3.ImageUrl = UrlHelper.GetSiteUrl() + ff[3];
                        img_3.Visible = true;
                    }
                    if (ff[4] != null)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[4]));
                        if (img.Width > 70) { img_4.Width = 70; img_4.Height = img.Height / (img.Width / 70); }
                        img_4.ImageUrl = UrlHelper.GetSiteUrl() + ff[4];
                        img_4.Visible = true;
                    }
                    if (ff[5] != null)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[5]));
                        if (img.Width > 70) { img_5.Width = 70; img_5.Height = img.Height / (img.Width / 70); }
                        img_5.ImageUrl = UrlHelper.GetSiteUrl() + ff[5];
                        img_5.Visible = true;
                    }
                    if (ff[6] != null)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[6]));
                        if (img.Width > 70) { img_6.Width = 70; img_6.Height = img.Height / (img.Width / 70); }
                        img_6.ImageUrl = UrlHelper.GetSiteUrl() + ff[6];
                        img_6.Visible = true;
                    }
                }
            }
            catch { }
        }

    }
